package cn.bdqn.service.Impl;

import cn.bdqn.dto.Dto;
import cn.bdqn.service.SerachService;
import cn.bdqn.util.Page;
import cn.bdqn.vo.AllHotelMessageVO;
import cn.bdqn.util.HotelDto;
import cn.bdqn.vo.HotCitySerachVO;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@Service
public class SerachServiceImpl implements SerachService {


    private HttpSolrClient httpSolrClient=new HttpSolrClient("http://localhost:8180/solr/hotel");


    @Override
    public Dto selectHotelAllMessage(AllHotelMessageVO allHotelMessageVO) {
        SolrQuery solrQuery=new SolrQuery();
        Dto dto=new Dto();
        int pageno=1;
        int pagesize=5;
        if(allHotelMessageVO.getPageNo()!=null){
            pageno=allHotelMessageVO.getPageNo();
        }
        if(allHotelMessageVO.getPageSize()!=null){
            pagesize= allHotelMessageVO.getPageSize();
        }
        if(allHotelMessageVO !=null){
            if(allHotelMessageVO.getDestination()!=null){
                solrQuery.setQuery("destination:"+ allHotelMessageVO.getDestination());
            }
            if(allHotelMessageVO.getKeywords()!=null&& allHotelMessageVO.getKeywords()!=""){
                solrQuery.setQuery("keyword:"+ allHotelMessageVO.getKeywords());
            }
            if(allHotelMessageVO.getHotelLevel()!=null){
                solrQuery.addFilterQuery("hotelLevel:"+ allHotelMessageVO.getHotelLevel());
            }
            if (allHotelMessageVO.getMaxPrice()!=null) {
                solrQuery.addFilterQuery("minPrice:" + "[* TO " + allHotelMessageVO.getMaxPrice() + "]");
            }
            if (allHotelMessageVO.getMinPrice()!=null) {
                solrQuery.addFilterQuery("minPrice:" + "[" + allHotelMessageVO.getMinPrice() + " TO *]");
            }
            if(allHotelMessageVO.getAscSort()!=null){
                solrQuery.setSort(allHotelMessageVO.getAscSort(), SolrQuery.ORDER.asc);
            }
            if(allHotelMessageVO.getDescSort()!=null){
                solrQuery.setSort(allHotelMessageVO.getDescSort(), SolrQuery.ORDER.desc);
            }
            if(allHotelMessageVO.getFeatureIds()!=null&&allHotelMessageVO.getFeatureIds()!=""){
                StringBuffer sb=new StringBuffer("(");
                int  flag=0;
                String [] features=allHotelMessageVO.getFeatureIds().split(",");
                for(String feature:features){
                    if(flag==0){
                        sb.append("featureIds:" + "*," + feature + ",*");
                    }else{
                        sb.append(" AND featureIds:" + "*," + feature + ",*");
                    }
                    flag++;
                }
                sb.append(")");
                solrQuery.addFilterQuery(sb.toString());
            }
            if(allHotelMessageVO.getTradeAreaIds()!=null&&allHotelMessageVO.getTradeAreaIds()!=""){
                StringBuffer sb=new StringBuffer("(");
                int  flag=0;
                String [] trades=allHotelMessageVO.getTradeAreaIds().split(",");
                for(String trade:trades){
                    if(flag==0){
                        sb.append("tradingAreaIds:" + "*," + trade + ",*");
                    }else{
                        sb.append(" AND tradingAreaIds:" + "*," + trade + ",*");
                    }
                    flag++;
                }
                sb.append(")");
                solrQuery.addFilterQuery(sb.toString());
            }
        }else{
            dto.setCode("10001");
            dto.setMsg("目的地不能为空1");
        }
        solrQuery.setStart((pageno-1)*pagesize);
        solrQuery.setRows(pagesize);
        QueryResponse response= null;
        try {
            response = httpSolrClient.query(solrQuery);
            dto.setCode("0");
            dto.setMsg("查询到了酒店列表");
        } catch (Exception e){
            dto.setCode("10002");
            dto.setMsg(e.getMessage());
            return dto;
        }
        List<HotelDto> list=response.getBeans(HotelDto.class);
        SolrDocumentList docs = response.getResults();
        Page page=new Page(pageno,Long.valueOf(docs.getNumFound()).intValue(),pagesize);
        page.setRows(list);
        page.setAllHotelMessageVO(allHotelMessageVO);
        dto.setData(page);
        solrQuery.clear();
        return dto;
    }

    @Override
    public Dto selectHotCityHotel(HotCitySerachVO hotCitySerachVO) {
        SolrQuery solrQuery=new SolrQuery();
        Dto dto=new Dto();
        solrQuery.setQuery("*:*");
        if(hotCitySerachVO.getCityId()!=null){
            solrQuery.addFilterQuery("cityId:" + hotCitySerachVO.getCityId());
        }else{
            dto.setCode("10002");
            dto.setMsg("没有传入城市编号");
            return  dto;
        }
        solrQuery.setStart(0);
        if(hotCitySerachVO.getCount()!=null){
            solrQuery.setRows(hotCitySerachVO.getCount());
        }else{
            dto.setCode("10001");
            dto.setMsg("没有传入需要显示的数量");
            return  dto;
        }
        QueryResponse response= null;
        try {
            response=httpSolrClient.query(solrQuery);
        } catch (Exception e){
            dto.setCode("10003");
            dto.setMsg(e.getMessage());
            return dto;
        }
        dto.setCode("0");
        dto.setMsg("热门城市酒店加载成功");
        List<HotelDto> hotelVOList=response.getBeans(HotelDto.class);
        dto.setData(hotelVOList);
        solrQuery.clear();
        return dto;
    }
}
